package com.cet.lxf.oop.test04;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-06-25 16:30
 **/
public class Main05 {
    public static void main(String[] args) {
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String insert = buildInsertSql(table, fields);
        System.out.println(insert);
        String s = "INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)";
        System.out.println(s.equals(insert) ? "测试成功" : "测试失败");
    }

    private static String buildInsertSql(String table, String[] fields) {

        StringBuilder insertSql = new StringBuilder("INSERT INTO ");

        String fieldsString = String.join(", ", fields);

        for (int i = 0; i < fields.length; i++) {
            fields[i] = "?";
        }

        String s = String.join(", ", fields);

        insertSql.append(table)
                 .append(" ").append("(").append(fieldsString).append(")")
                 .append(" VALUES")
                 .append(" ").append("(").append(s).append(")");

        return insertSql.toString();
    }
}
